package example1;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * <h3>designPattern</h3>
 * <p>Map中迭代器的使用</p>
 * @author : zhang.bw
 * @date : 2021-02-27 16:21
 **/
public class MapIterator {

	public static void main(String[] args) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("one", "第一");
		map.put("two", "第二");
		map.put("three", "第三");
		map.put("hh", "憨憨");

		//1、迭代器
		Iterator<Map.Entry<String, Object>> iter = map.entrySet().iterator();
		//判断往下还有没有数据
		while(iter.hasNext()){
			//有的话取出下面的数据
			Map.Entry<String, Object> entry = iter.next();
			String key = entry.getKey();
			String value = (String)entry.getValue();
			if("hh".equals(entry.getKey())){
				System.out.println("这个是憨憨");
			}
			System.out.println(key + " ：" + value);
		}

		//2、foreach循环
		//获取key + value
		for (Object key : map.keySet()) {
			String value = (String)map.get(key);
			System.out.println(key + " : " + value);
		}
		//获取value
		for (Object value : map.values()) {
			System.out.println(value);
		}

		//3、当容量特别大的时候
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			System.out.println(entry.getKey() + " : " + entry.getValue());
		}
	}
}
